package com.djakapm.swing.component.textarea;

import com.djakapm.swing.common.ContainerFactory;
import com.djakapm.swing.container.FlowPanel;

import javax.swing.*;
import java.awt.*;

class TextAreaLineNumberPanel extends FlowPanel {
    private int lineNumber;
    private JPanel lineNumberPanel;
    private int currentLineNumber;

    public TextAreaLineNumberPanel(){
        super(FlowLayout.RIGHT,10,0);
        this.lineNumberPanel = ContainerFactory.newGridLayoutedPanel(0,1);
        this.setPreferredSize(new Dimension(50,0));
        this.add(this.lineNumberPanel);
    }


    public Component[] getComponents(){
        return this.lineNumberPanel.getComponents();
    }

    protected int getLineNumber(){
        return this.lineNumber;
    }
    
    protected TextAreaLineNumberPanel setLineNumber(int lineNumber) {
        this.lineNumber = lineNumber;
        return this;
    }

    private TextAreaLineNumberPanel displayLineNumberOnExistingComponents(Component[] components, int startIndex,int endIndex){
        for(int idx=startIndex;idx<endIndex;idx++){
            JLabel label = (JLabel) components[idx];
            label.setText((idx+1)+"");
        }
        return this;
    }


    private TextAreaLineNumberPanel insertNewLabelForLineNumber(int startIndex,int endIndex,JPanel numberBar){
        for(int idx=startIndex;idx < endIndex;idx++){
            if(endIndex > idx){
                numberBar.add(new TextAreaRowNumberLabel(idx+1));
            }
        }
        return this;
    }

    protected TextAreaLineNumberPanel addLineNumber(){
        if(noNewLine()){ return this;}
        Component[] components = this.lineNumberPanel.getComponents();

        if(components.length >= this.lineNumber){
            this.displayLineNumberOnExistingComponents(components,this.currentLineNumber,this.lineNumber);
        }
        else{
            this.insertNewLabelForLineNumber(components.length,this.lineNumber,this.lineNumberPanel);
        }
        this.updateCurrentLineNumber();
        return this;
    }

    private TextAreaLineNumberPanel setLineNumberToEmptyString(Component[] components, int startIndex,int endIndex){
        for(int idx=startIndex;idx<endIndex;idx++){
            JLabel label = (JLabel) components[idx];
            label.setText("");
        }
        return this;
    }
    
    protected TextAreaLineNumberPanel removeLineNumber(){
        if(noNewLine()){ return this;}
        Component[] components = this.lineNumberPanel.getComponents();
        if(components.length > 0 && components.length > this.lineNumber){
            this.setLineNumberToEmptyString(components,this.lineNumber,this.currentLineNumber);
        }

        this.revalidate();
        this.repaint();
        this.updateCurrentLineNumber();
        return this;
    }

    private TextAreaLineNumberPanel updateCurrentLineNumber(){
        this.currentLineNumber = this.lineNumber;
        return this;
    }

    private boolean noNewLine(){
        return (this.currentLineNumber == this.lineNumber);
    }
}
